import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        redirect: '/login',
    },
    {
        path: '/register',
        component: () =>
            import(/* webpackChunkName: "about" */ '../views/Register.vue'),
    },
    {
        path: '/login',
        component: () =>
            import(/* webpackChunkName: "about" */ '../views/Login.vue'),
    },
    {
        path: '/qiandao',
        component: () =>
            import(/* webpackChunkName: "about" */ '../views/Qiandao.vue'),
    },
];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
});

router.beforeEach((to, from, next) => {
    // console.log('to', to);
    // console.log('from', from);
    if (to.path === '/login' || to.path === '/register') {
        next();
    } else {
        if (window.sessionStorage.getItem('user')) {
            next();
        } else {
            next('/login');
        }
    }
});

export default router;
